import lFile from '@/uni_modules/l-file/components/l-file/l-file.vue'
export default {
	components: {
		lFile
	},
	data() {
		return {

			//用户信息
			userId: '',
			dataLocation: '',
			nickName: '',
			token: '',
			headImg: '',

			//文件操作菜单
			optList: [{
				text: '删除',
				color: 'red'
			}, {
				text: '复制',
				color: 'blue'
			}, {
				text: '重命名'
			}],
			
			//用户文件总数
			fileNum:0,
			//用户文件总大小
			fileLen:0,
			//用户存储上限
			fileTopLen:0,

			//选中的文件路径
			optFilePath: '',
			//选中的文件名
			optFileName: '',
			//选中文件是否为目录
			optFileIsDirectory: false,


			//文件重命名-新文件名
			newFileName: '',


			//复制的文件路径
			copyFilePath: '',
			//复制的文件名
			copyFileName: '',


			//是否打开文件操作菜单
			optShow: false,
			//是否打开创建目录菜单
			mkdirShow: false,
			renameShow: false,
			deleteShow: false,

			//广告牌
			adList: [
				'https://dpwgc-1302119999.cos.ap-guangzhou.myqcloud.com/dpwgc_im/head/2021/8/17/head_6fd172cd-b1bf-4f19-a4a1-19484af62ce8.jpg',
			],

			//当前目录
			path: '/',

			//新建目录名
			newPath: '',

			//文件列表
			fileList: [],

            current: 0,
			downList: [{
					iconPath: "file-text",
					selectedIconPath: "file-text-fill",
					text: '文件',
					customIcon: false,
					pagePath: '/pages/index/index',
				},
				{
					iconPath: "edit-pen",
					selectedIconPath: "edit-pen-fill",
					text: '笔记',
					customIcon: false,
					pagePath: '/pages/notepad/notepad'
				},
				{
					iconPath: "account",
					selectedIconPath: "account-fill",
					text: '我的',
					customIcon: false,
					pagePath: '/pages/user/user'
				}
			],
		}
	},
	onShow() {
		this.headImg = uni.getStorageSync('headImg');
		this.nickName = uni.getStorageSync('nickName');
		this.userId = uni.getStorageSync('userId');
		this.token = uni.getStorageSync('token');
		this.userUuid = uni.getStorageSync('userUuid');
		this.phone = uni.getStorageSync('phone');
		this.email = uni.getStorageSync('email');
		this.dataLocation = uni.getStorageSync('dataLocation');

		setTimeout(() => {
			this.getFileList();
			this.countFile();
		}, 500);
	},
	methods: {
		async beforeSwitch(index) {
			switch (index) {
				case 0:
					break;
				case 1:
					break;
				case 2:
					break;
			}
			return true;
		},
		//操作菜单
		opt(index) {
			console.log(index)
			switch (index) {
				case 0:
					this.deleteShow = true;
					break;
				case 1:
					this.checkFileCopy();
					break;
				case 2:
					this.renameShow = true;
					break;
			}
		},
		//下载
		downloadFile(fileName) {

			//如果是空字符串
			if (this.path === "") {
				this.path = "/";
			}

			var url = this.$url + '/Hdfs/downloadFile?path=' + this.dataLocation + this.path + "/" + fileName;

			this.$refs.lFile.download({
					url, //必填，附件网络地址
					type: 'save', //选填，非save为临时下载
					customName: "_downloads/beFile/"+fileName,
					//...DownloadOptions直接写key:value 
					method: 'GET'
				})
				.then(path => {
					this.localPath = path;
				});
		},
		//上传
		onUpload() {

			let that = this;

			this.$refs.lFile.upload({
				// #ifdef APP-PLUS
				//（app端必传）nvue页面使用时请查阅nvue获取当前webview的api，当前示例为vue窗口
				currentWebview: this.$mp.page.$getAppWebview(),
				// #endif
				//替换为你的上传接口地址
				url: this.$url + '/Hdfs/uploadFile',
				// 服务端接收附件的key
				name: 'file',
				//根据你接口需求自定义 (优先不传content-type,安卓端无法收到参数再传)
				header: {
					'userId': that.userId,
					'token': that.token
				},
				'path': this.dataLocation + "/" + this.path,

				// 限制选择附件的大小上限，默认10M
				// maxSize: 20,

				// 若需要在body单独添加附件名或附件大小如下方式传入组件：
				// addName: '后端要的附件名称字段key,此处请勿写name的同值如(file)，会覆盖name',
				// addSize: '后端要的附件大小字段key'

				// body参数直接写key,value,如：
				// date: '2020-1-1',
				// key2: 'value2',
			});
			/*
			uni.chooseFile({
				count: 1, //默认100
				success: function(res) {

					const tempFilePaths = res.tempFilePaths;
					uni.uploadFile({
						url: that.$url + '/Hdfs/uploadFile',
						filePath: tempFilePaths[0],
						name: 'file',
						header: {
							'userId': that.userId,
							'token': that.token
						},
						formData: {
							'path': that.dataLocation + "/" + that.path
						},
						success: (res) => {
							uni.showToast({
								title: "文件上传成功",
								icon: 'none',
								duration: 2000
							})
							that.getFileList();
						},
						fail: (res) => {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
						}
					});
				}
			});
			*/
		},
		onSuccess(res) {

			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
			this.getFileList();
		},
		//打开新建文件夹窗口
		openMkdir() {
			this.mkdirShow = true;
		},
		//新建文件夹
		mkdir() {

			//如果是空字符串
			if (this.path === "") {
				this.path = "/";
			}

			this.$request('/Hdfs/mkdir', {
					"path": this.dataLocation + this.path + "/" + this.newPath
				})
				.then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
						this.path = this.path + "/" + this.newPath;
						this.getFileList();
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
				.catch(e => {
					uni.showToast({
						title: '错误',
						icon: 'none',
						duration: 2000
					});
				});
		},
		//打开文件重命名窗口
		openRenameFile() {
			this.renameShow = true;
		},
		//文件重命名
		renameFile() {

			//如果是空字符串
			if (this.path === "") {
				this.path = "/";
			}

			//新保存路径
			var newPath = "";

			if (this.path === "/") {
				newPath = "/" + this.newFileName;
			} else {
				newPath = this.path + "/" + this.newFileName;
			}


			this.$request('/Hdfs/renameFile', {
					"oldPath": this.dataLocation + this.optFilePath,
					"newPath": this.dataLocation + newPath
				})
				.then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
						this.getFileList();
						this.renameShow = false;
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
				.catch(e => {
					uni.showToast({
						title: '错误',
						icon: 'none',
						duration: 2000
					});
				});
		},
		//打开文件删除确认框
		openDeleteFile() {
			this.deleteShow = true;
		},
		//删除文件
		deleteFile() {
			this.$request('/Hdfs/deleteFile', {
					"path": this.dataLocation + this.optFilePath
				})
				.then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
						this.getFileList();
						this.deleteShow = false;
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
				.catch(e => {
					uni.showToast({
						title: '错误',
						icon: 'none',
						duration: 2000
					});
				});
		},
		//选中文件，复制到剪切板
		checkFileCopy() {

			if (this.optFileIsDirectory == true) {
				uni.showToast({
					title: "不支持文件目录复制",
					icon: 'none',
					duration: 2000
				});
				return;
			}
			this.copyFileName = this.optFileName;

			if (this.path === "") {
				this.path = "/";
			}

			if (this.path === "/") {
				this.copyFilePath = "/" + this.copyFileName;
			} else {
				this.copyFilePath = this.path + "/" + this.copyFileName;
			}
		},
		//复制文件
		copyFile() {

			if (this.copyFileName === "") {
				uni.showToast({
					title: "未选择文件",
					icon: 'none',
					duration: 2000
				});
				return;
			}

			//粘贴目标
			var targetPath = "";
			//原文件位置
			var sourcePath = this.copyFilePath;


			if (this.path === "") {
				this.path = "/";
			}

			if (this.path === "/") {
				targetPath = "/" + this.copyFileName;
			} else {
				targetPath = this.path + "/" + this.copyFileName;
			}

			this.$request('/Hdfs/copyFile', {
					"targetPath": this.dataLocation + targetPath,
					"sourcePath": this.dataLocation + sourcePath
				})
				.then(res => {
					if (res.code == 200) {
						this.getFileList();
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
				.catch(e => {
					console.log(e)
				});
		},
		//打开文件设置界面
		openFileOpt(e) {
			this.optShow = true;
			this.optFileName = e.filePath.name;
			this.newFileName = this.optFileName;
			this.optFileIsDirectory = e.isDirectory;

			if (this.path === "") {
				this.path = "/";
			}

			if (this.path === "/") {
				this.optFilePath = "/" + this.optFileName;
			} else {
				this.optFilePath = this.path + "/" + this.optFileName;
			}
		},
		//获取文件列表
		getFileList() {

			//如果是空字符串
			if (this.path === "") {
				this.path = "/";
			}

			this.$request('/Hdfs/readPathInfo', {
					"path": this.dataLocation + this.path
				})
				.then(res => {
					if (res.code == 200) {
						this.fileList = res.data;
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
				.catch(e => {
					console.log(e)
				});
		},
		//打开文件夹
		openFile(e) {

			//如果是根目录
			if (this.path === "/") {
				this.path = "";
			}

			this.path = this.path + '/' + e;

			this.$request('/Hdfs/readPathInfo', {
					"path": this.dataLocation + this.path
				})
				.then(res => {
					if (res.code == 200) {
						this.fileList = res.data;
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
				.catch(e => {
					console.log(e)
				});
		},
		//返回到上级文件夹
		backFile() {

			var arrPath = this.path.split("/");
			arrPath.pop();
			var backPath = "";
			for (let i in arrPath) {
				backPath = backPath + arrPath[i];
			}
			this.path = "/" + backPath;

			this.$request('/Hdfs/readPathInfo', {
					"path": this.dataLocation + this.path
				})
				.then(res => {
					if (res.code == 200) {
						this.fileList = res.data;
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
				.catch(e => {
					console.log(e)
				});
		},
		//统计文件总数及总大小
		countFile() {
		
			this.$request('/Hdfs/countFile', {
					"path": this.dataLocation,
					"userId":this.userId
				})
				.then(res => {
					if (res.code == 200) {
						this.fileNum = res.data.fileNum;
						this.fileLen = res.data.fileLen;
						this.fileTopLen = res.data.fileTopLen;
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
				.catch(e => {
					console.log(e)
				});
		},
	}
}
